package com.yayohos.locationmanager.fractions;

import com.yayohos.locationmanager.ResourceTable;
import com.yayohos.locationmanager.SampleFractionAbility;
import com.yayohos.locationmanager.SamplePresenter;
import com.yayohos.locationmanager.base.LocationBaseFraction;
import com.yayohos.locationmanager.configuration.Configurations;
import com.yayohos.locationmanager.configuration.LocationConfiguration;
import com.yayohos.locationmanager.helper.LogUtils;

import ohos.aafwk.content.Intent;

import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

import ohos.location.Location;

/**
 * 在Fraction中定位
 *
 * @date 2021/5/26
 */
public class SimpleFraction extends LocationBaseFraction implements SamplePresenter.SampleView {
    private SamplePresenter samplePresenter;
    private Text text;
    private Button do_location;
    private SampleFractionAbility sampleFractionAbility;
    private LocationBaseFraction.PermissionCallBack permissionCallBack;

    @Override
    public int getRootComponent() {
        return ResourceTable.Layout_layout_sample_fraction;
    }

    @Override
    public void initComponent(Component component) {
        text = (Text) component.findComponentById(ResourceTable.Id_text_helloworld);
        do_location = (Button) component.findComponentById(ResourceTable.Id_do_location);
        do_location.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                getLocation();
            }
        });
    }

    @Override
    public LocationConfiguration getLocationConfiguration() {
        return Configurations.defaultConfiguration("请授予定位权限", "请打卡GPS服务");
    }


    @Override
    public void onLocationFailed(int type) {
        super.onLocationFailed(type);
        samplePresenter.onLocationFailed(type);
    }

    @Override
    public void onLocationChanged(Location location) {
        super.onLocationChanged(location);
        samplePresenter.onLocationChanged(location);
    }

    @Override
    public void onProcessTypeChanged(int processType) {
        super.onProcessTypeChanged(processType);
        samplePresenter.onProcessTypeChanged(processType);
    }

    @Override
    protected void onStart(Intent intent) {
        LogUtils.logI("SimpleFraction==onStart");
        super.onStart(intent);
        sampleFractionAbility = (SampleFractionAbility) getFractionAbility();
        samplePresenter = new SamplePresenter(this);
        permissionCallBack = new LocationBaseFraction.PermissionCallBack() {
            @Override
            public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
                LogUtils.logI("abilitySliceCallBack");
                getLocationManager().onRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        };
        sampleFractionAbility.setPermissionCallBack(permissionCallBack);
    }

    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onComponentDetach() {
        super.onComponentDetach();
    }

    @Override
    protected void onBackground() {
        super.onBackground();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public String getText() {
        return text.getText();
    }

    @Override
    public void setText(String text1) {
        text.setText(text1);
    }

    @Override
    public void updateProgress(String text) {
        LogUtils.logI(text);
    }

    @Override
    public void dismissProgress() {
    }
}
